<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :okAuth="'device:management:inDeviceManagement:edit'"
    @register="registerDrawerDetail"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
    <SeniorList :tid="seniorListId" :hostId="HostId" :equipmentCode="EquipmentCode" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsDeviceManagementInDeviceManagementForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { InDeviceManagement, inDeviceManagementSave, inDeviceManagementForm } from '/@/api/device.management/inDeviceManagement';
  import {Office, officeTreeData,officeListData,officeTreeDataList} from '/@/api/sys/office';
  import {unitRegionListData} from '/@/api/unitmanage/unitRegion';
  import {unitBuildingListData} from '/@/api/unitmanage/unitBuilding';
  import SeniorList from './senior/list.vue'

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('device.management.inDeviceManagement');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<InDeviceManagement>({} as InDeviceManagement);
  const seniorListId=ref()
  const HostId=ref()
  const EquipmentCode=ref()

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('详情') : t('详情'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('设备名称'),
      field: 'equipmentName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
      required: true,
    },
    {
      label: t('设备编码'),
      field: 'equipmentCode',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
      required: true,
    },
    // {
    //   label: t('设备类型id'),
    //   field: 'equipmentTypeId',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 255,
    //   },
    //   required: true,
    // },
    {
      label: t('设备类型'),
      field: 'equipmentType',
      component: 'Select',
      componentProps: {
        maxlength: 255,
        dictType: 'testing_equipment_type',
        onChange:(dictLabelRaw,id)=>{
          record.value.equipmentTypeId=id,
          record.value.equipmentTypeName=dictLabelRaw
        },
        fieldNames:{
          label:'dictLabelRaw',
          value:'id'
        },
        allowClear:true,
        disabled: true
      },
      required: true,
    },
    {
      label: t('所属主机'),
      field: 'hostId',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
      required: true,
    },
    // {
    //   label: t('企业id'),
    //   field: 'enterpriseId',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 255,
    //   },
    //   required: true,
    // },
    {
      label: t('企业名称'),
      field: 'enterpriseId',
      fieldLabel:'enterpriseName',
      component: 'TreeSelect',
      componentProps: {
        maxlength: 255,
        api:officeTreeData,
         params:{
        },
        onChange:(code)=>{
          record.value.enterpriseId=code
        },
        fieldNames:{
          label:"name", 
          value:"code" 
        },
        immediate:true,
        allowClear: true,
        disabled: true
      },
      required: true,
    },
    // {
    //   label: t('安装建筑id'),
    //   field: 'buildId',
    //   component: 'Input',
    //   rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    // },
    {
      label: t('安装建筑名称'),
      field: 'buildId',
      fieldLabel:'buildName',
      component: 'Select',
      componentProps: {
        maxlength: 255,
        api:unitBuildingListData,
        onChange:(id,name)=>{
          record.value.buildId=id,
          record.value.buildName=name
        },
        resultField:"list", // { list:[] }
        fieldNames:{
          label:"name", // label
          value:"id" // value
        },
        // immediate:true,
        allowClear:true,
        disabled: true
      },
      required: true,
    },
    // {
    //   label: t('区域Id'),
    //   field: 'regionId',
    //   component: 'Input',
    //   rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    // },
    {
      label: t('区域名称'),
      field: 'regionId',
      fieldLabel:'regionName',
      component: 'Select',
      componentProps: {
        maxlength: 255,
        api:unitRegionListData,
        onChange:(id)=>{
          record.value.regionId=id
          // record.value.regionName=name
        },
        resultField:"list", // { list:[] }
        fieldNames:{
          label:"name", // label
          value:"id" // value
        },
        // immediate:true,
        allowClear:true,
        disabled: true
      },
      required: true,
    },
    {
      label: t('详细地址'),
      field: 'address',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
      required: true,
    },
    {
      label: t('当前设备实时状态'),
      field: 'realStatus',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('示例图'),
      field: 'image',
      component: 'Upload',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('防火分区'),
      field: 'firePartition',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawerDetail, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await inDeviceManagementForm(data);
    seniorListId.value = res.inDeviceManagement?.tid || null;
    HostId.value = res.inDeviceManagement?.hostId || null
    EquipmentCode.value = res.inDeviceManagement?.equipmentCode || null
    record.value = (res.inDeviceManagement || {}) as InDeviceManagement;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
        equipmentTypeId: record.value.equipmentTypeId,
        regionId: record.value.regionId,
        buildId: record.value.buildId,
        // enterpriseId: record.value.enterpriseId,
      };
      // console.log('submit', params, data, record);
      const res = await inDeviceManagementSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
